﻿using MvcPaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.Entity;
using zichan.DAL;
using zichan.BLL.Interface;

namespace zichan.BLL
{
   public class CompanyClassService
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string AddCompany(CompanyClass model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try {
                    if (db.CompanyClasses.Count(o => o.Name == model.Name) > 0)
                        return model.Name + "已存在";
                    db.CompanyClasses.Add(model);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch(Exception ex)
                {
                    return "添加失败，请重新添加";
                }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string EditCompany(CompanyClass model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.CompanyClasses.Count(o => o.Id != model.Id && o.Name == model.Name) > 0)
                    return model.Name + "已存在";
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return string.Empty;
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string RemoveCompany(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                CompanyClass m = db.CompanyClasses.FirstOrDefault(o => o.Id == id);
                if (m != null)
                {
                    if (db.Assets.Count(o => o.CompanyId == id) > 0)
                    {
                        return m.Name + "不能删除，已在使用中";
                    }
                    db.CompanyClasses.Remove(m);
                    db.SaveChanges();
                    ClassLogService.AddLog("公司分类", "删除"+"\""+m.Name+"\"");
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static CompanyClass GetCompany(int Id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.CompanyClasses.FirstOrDefault(o => o.Id == Id);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static IPagedList<CompanyClass> GetCompanyList(string name, int pageSize, int pageIndex)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<CompanyClass> w = db.CompanyClasses;
                if (!string.IsNullOrEmpty(name))
                    w = w.Where(o => o.Name.Contains(name));

                List<CompanyClass> list = w.OrderBy(o => o.Sort).ThenBy(o => o.Id).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                foreach(CompanyClass c in list)
                {
                    c.UseCount = db.Assets.Count(o => o.CompanyId == c.Id) + db.AssetTemps.Count(o => o.CompanyId == c.Id);
                }
                return list.OrderBy(o => o.Sort).ThenBy(o => o.Id).ToPagedList(pageIndex - 1, pageSize, w.Count());
            }
        }
        public static List<CompanyClass> GetCompanyList()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.CompanyClasses.OrderBy(o=>o.Sort).ThenBy(o => o.Id).ToList();
            }
        }
       /// <summary>
       /// 批量添加
       /// </summary>
       /// <param name="nameList">输入的名称集合</param>
       /// <param name="existName">重复的名称</param>
       /// <param name="nameListForLog">保存入数据库的名称，用以做日志使用</param>
       /// <returns></returns>
        public static string AddBatchClass(List<string> nameList,out List<string> existName,out List<string> nameListForLog)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try {
                    existName=new List<string>();
                    nameListForLog = new List<string>();
                    int maxSort = db.CompanyClasses.Max(o => o.Sort);
                    List<CompanyClass> classList = new List<CompanyClass>();
                    foreach (string n in nameList)
                    {
                        if (db.CompanyClasses.Count(o => o.Name == n) > 0||classList.Count(o=>o.Name==n)>0)
                        {
                            existName.Add(n);
                            continue;
                        }
                        nameListForLog.Add(n);
                        CompanyClass model = new CompanyClass();
                        maxSort += 1;
                        model.Name = n;
                        model.Sort = maxSort;
                        classList.Add(model);
                    }
                    if (classList.Count() > 0)
                    {
                        db.CompanyClasses.AddRange(classList);
                        db.SaveChanges();
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    existName =new List<string>();
                    nameListForLog = new List<string>();
                    return "无法添加，请重试";
                }
            }
        }
        public static int MaxSort()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.CompanyClasses.Count() > 0)
                    return db.CompanyClasses.Max(o => o.Sort);
                else
                    return 0;
            }
        }
    }
}
